package EWCore.Items
{
	import EWCore.ItemDefinitions.Item;
	import EWCore.Items.Attributes.Attribute;
	import EWCore.Items.Attributes.AttributeType;
	import EWCore.Utilities.Useful;

	public class Item
	{
		public var m_Name:String;				// 名称
		public var m_Quality:int;				// 品质
		public var m_IconIndex:int;				// 图标
		public var m_Type:String;				// 类型
		//public var m_Group:ItemType;			// 分组
		public var m_FixedAttributes:Array=[];	// 固定属性
		public var m_Attributes:Array=[];		// 属性
		public var m_Level:int;					// 等级
		public static var QUALITYPREFIX = ["","荒·","改·","真·","極·"];
		public static var ITEMLIST:Object;
		public function Item()
		{
		}
		
		public static function GetItemByName(i_ShortName:String,i_Quality:int):Item
		{
			var item:Item = new Item();
			item.m_Name = ITEMLIST[i_ShortName].fullname;
			item.m_Quality = i_Quality;
			item.m_Level = ITEMLIST[i_ShortName].level;
			item.m_IconIndex = ITEMLIST[i_ShortName].icon;
			item.m_Type = ITEMLIST[i_ShortName].itemtype;
			var t_FixedAttrList = (ITEMLIST[i_ShortName].attribute as String).split("|");
			for each(var t_FixedAttr:String in t_FixedAttrList)
			{
				var t_FixedAttrKVPair:Array = t_FixedAttr.split(":");
				item.AddFixedAttribute(t_FixedAttrKVPair[0],t_FixedAttrKVPair[1]);
			}
			return item;
		}
		
		/**
		 * 给物品增加指定属性
		 * */
		public function AddAttributeByName(i_ShortName:String):void
		{
			var t_Attr:Attribute = Attribute.CreateAttribute(i_ShortName,this.m_Level, this.m_Quality);
			this.AddAttribute(t_Attr);
		}
		/**
		 * 给物品增加随机属性
		 * @param i_Quality 属性品质
		 * */
		public function AddAttributeRandom(i_Quality:int=-1):void
		{
			if(i_Quality==-1)
				i_Quality = this.m_Quality;
			var t_Attr:Attribute = Attribute.CreateAttributeByType(this.m_Type, this.m_Level, i_Quality, this.m_Quality);
			trace("ADD:",t_Attr);
			this.AddAttribute(t_Attr);
		}
		public function AddFixedAttribute(i_ShortName:String,i_Value:String):void
		{
			var t_Attr:Attribute = Attribute.CreateAttribute(i_ShortName, this.m_Level, this.m_Quality);
			t_Attr.m_Value = Useful.CalcExpression(i_Value,["LV","PZ"],[this.m_Level, this.m_Quality]);
			this.m_FixedAttributes.push(t_Attr);
		}
		/**
		 * 给物品增加属性
		 * @param i_Attribute 属性
		 * */
		private function AddAttribute(i_Attribute:Attribute):void
		{
			if(i_Attribute==null || i_Attribute.m_Value==0)
				return;
			for(var i=0; i<this.m_Attributes.length; i++)
			{
				if(i_Attribute.m_AttributeType.IsEqualTo((this.m_Attributes[i] as Attribute).m_AttributeType))
				{
					this.m_Attributes[i].m_Value+=i_Attribute.m_Value;
					return;
				}
			}
			this.m_Attributes.push(i_Attribute);
		}
		
		public function toString():String
		{
			var t_Rtn:String = "-------------------------------------\n"+QUALITYPREFIX[this.m_Quality]+this.m_Name+"\n[固定属性]\n";
			for(var i=0; i<this.m_FixedAttributes.length; i++)
			{
				t_Rtn+=this.m_FixedAttributes[i].toString();
			}
			t_Rtn+="[属性加成]\n";
			for(i=0; i<this.m_Attributes.length; i++)
			{
				t_Rtn+=this.m_Attributes[i].toString();
			}
			return t_Rtn;
		}
	}
}